package uk.ac.cam.tcs40.sidnet.formal;

import java.util.LinkedList;
import java.util.List;

import uk.ac.cam.tcs40.sidnet.R;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class GuestAdapter extends ArrayAdapter<Guest> {

	private final Context m_Context;
	private final LayoutInflater m_Inlater;
	private final List<Guest> m_Values;
	
	private Formal m_Formal;

	public GuestAdapter(Context context, List<Guest> list) {
		super(context, R.layout.guest_list_item, list);
		m_Context = context;
		m_Inlater = (LayoutInflater) m_Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		m_Values = list;
	}
	
	public void setFormal(Formal formal) {
		if (m_Formal ==null)
			m_Formal = formal;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final View view = m_Inlater.inflate(R.layout.guest_list_item, parent, false);

		final Guest guest = m_Values.get(position);
		
		final AutoCompleteTextView name = (AutoCompleteTextView) view.findViewById(R.id.guest_name);
		name.setText(guest.getName());
		name.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (guest != null)
					guest.setName(s.toString());
			}
		});
		
		
		// Get all of the user's contacts, and set an adapter on the AutoCompleteTextView with the contact names. 
		String[] projection = { ContactsContract.Contacts.DISPLAY_NAME };
		Cursor cursor = m_Context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);

		List<String> contacts = new LinkedList<String>();

		if (cursor != null) {
			while (cursor.moveToNext()) {
				String contactName = cursor.getString(0);
				contacts.add(contactName);
			}
			cursor.close();
		}
		name.setAdapter(new ArrayAdapter<String>(m_Context, android.R.layout.simple_list_item_1, contacts));
		
		final Spinner dietary = (Spinner) view.findViewById(R.id.guest_dietary_requirements);	
		dietary.setAdapter(new ArrayAdapter<Dietary>(m_Context, android.R.layout.simple_list_item_1, Dietary.values()));
		dietary.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
				if (guest != null)
					guest.setDietary(Dietary.values()[position]);
				
				if (Dietary.values()[position] == Dietary.OTHER) {
					view.findViewById(R.id.guest_notes).setVisibility(View.VISIBLE);
				} else {
					view.findViewById(R.id.guest_notes).setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		dietary.setSelection(guest.getDietary().ordinal());

		final EditText notes = (EditText) view.findViewById(R.id.guest_notes);
		notes.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (guest != null)
					guest.setNotes(s.toString());
			}
		});
		notes.setText(guest.getNotes());
		
		final ImageButton deleteGuest = (ImageButton) view.findViewById(R.id.delete_guest);
		deleteGuest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (m_Formal != null)
					m_Formal.removeGuest(position);
				
				notifyDataSetChanged();
			}
		});
		
		return view;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}
}
